package com.adinnet.admin.rankLevel.controller;


import com.adinnet.admin.common.service.CommonService;
import com.adinnet.admin.rankLevel.model.RankLevel;
import com.adinnet.admin.rankLevel.service.RankLevelService;
import com.adinnet.core.BaseController;
import com.adinnet.core.JsonResult;
import com.adinnet.core.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 段位等级表 前端控制器
 * </p>
 *
 * @author zYshuai
 * @since 2020-12-18
 */
@Controller
@RequestMapping("admin/rankLevel")
public class RankLevelController extends BaseController {

    @Autowired
    private RankLevelService rankLevelService;

    @Autowired
    private CommonService commonService;

    @GetMapping
    public String index(Model model) {
        return "rankLevel/rankLevel.html";
    }

    /**
     * @Description: 段位列表
     * @author zYshuai
     * @Date: 2020/12/18 10:49
     * @return: com.adinnet.core.PageResult<com.adinnet.admin.peaseRule.model.PeaseRule>
     */
    @ResponseBody
    @RequestMapping("/list")
    public PageResult<RankLevel> list(Integer page, Integer limit) {
//        List<RankLevel> rankLevels = rankLevelService.list(new QueryWrapper<RankLevel>().eq("deleted", 0));
//        return new PageResult<>(rankLevels);
        if (page == null || limit == null) {
            page = 1;
            limit = 10;
        }
        IPage<RankLevel> iPage = rankLevelService.page(new Page<>(page, limit), new QueryWrapper<RankLevel>().eq("deleted", 0));
        return new PageResult<>(iPage.getRecords(), iPage.getTotal());
    }


    /**
     * @param flag:     edit-修改  add-新增
     * @param rankLevel
     * @Description: 新增和修改
     * @author zYshuai
     * @Date: 2020/12/18 10:54
     * @return: com.adinnet.core.JsonResult
     */
    @ResponseBody
    @RequestMapping("/crud")
    @Transactional
    public JsonResult crud(String flag, RankLevel rankLevel, HttpServletRequest request) {
        if ("edit".equals(flag)) {
            rankLevelService.updateById(rankLevel);
            commonService.saveOperation(request, "修改段位管理，ID：" + rankLevel.getId());
        }
        if ("add".equals(flag)) {
            rankLevelService.save(rankLevel);
            commonService.saveOperation(request, "新增段位管理，ID：" + rankLevel.getId());
        }
        return ok("操作成功");
    }

}
